package com.medical.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.medical.core.result.Result;
import com.medical.system.model.form.DeptForm;
import com.medical.system.model.query.DeptQuery;
import com.medical.system.model.vo.DeptVO;
import com.medical.system.service.SysDeptService;
import com.medical.web.model.Option;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "04.部门接口")
@RestController
@RequestMapping("/api/v1/dept")
@AllArgsConstructor
public class SysDeptController {

    private final SysDeptService sysDeptService;

    @Operation(summary = "获取部门列表")
    @GetMapping("/list")
    public Result<List<DeptVO>> getDeptList(@ParameterObject DeptQuery deptQuery) {
        return Result.success(sysDeptService.getDeptList(deptQuery));
    }

    @Operation(summary = "获取部门下拉列表")
    @GetMapping("/options")
    public Result<List<Option>> getDeptOptions() {
        return Result.success(sysDeptService.getDeptOptions());
    }

    @Operation(summary = "获取部门表单数据")
    @GetMapping("/{deptId}/form")
    public Result<DeptForm> getDeptForm(@Parameter(description ="部门ID") @PathVariable  Long deptId) {
        return Result.success(sysDeptService.getDeptForm(deptId));
    }

    @Operation(summary = "保存部门信息")
    @PostMapping
    @SaCheckPermission("sys:dept:add")
    public Result saveDept(@Valid @RequestBody DeptForm deptForm) {
        return Result.success(sysDeptService.saveDept(deptForm));
    }

    @Operation(summary = "更新部门信息")
    @PutMapping("/{deptId}")
    @SaCheckPermission("sys:dept:update")
    public Result updateDept(@Parameter(description ="部门ID") @PathVariable Long deptId, @Valid @RequestBody DeptForm deptForm) {
        return Result.success(sysDeptService.updateDept(deptId, deptForm));
    }

    @Operation(summary = "删除部门信息")
    @DeleteMapping("/{ids}")
    @SaCheckPermission("sys:dept:delete")
    public Result deleteDepts(@Parameter(description = "部门ID，多个以英文逗号(,)分割") @PathVariable String ids) {
        return Result.judge(sysDeptService.deleteByIds(ids));
    }
}
